import matplotlib.patches as mpatches
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm


def read_flm_data(file):
    all_row_data = []

    with open(file, 'r') as file:
        # Process the data section
        starter = False
        for line in file:
            if line.startswith('*Y\X'):
                starter = True
                continue
            if starter:
                row_data = list(map(float, line.strip().split()[1:]))
                all_row_data.append(row_data)

    return all_row_data


if __name__ == "__main__":
    file_path = "data\I_5_10_8_6_5_m.flm"
    data = read_flm_data(file_path)
    data_array = np.array(data)
    colors = ['#0000B6', '#0023fe', '#016db7', '#31b56d', '#64ff23', '#95b600', '#ca6e00', '#fe2400']
    bounds = [0, 500, 1000, 1500, 2000, 2125, 2250, 2375, 2500]  # Add a new boundary for the new color
    custom_cmap = ListedColormap(colors).with_extremes(over='0.25')
    norm = BoundaryNorm(bounds, custom_cmap.N, clip=True)

    aspect_ratio = data_array.shape[1] / data_array.shape[0]

    fig, ax = plt.subplots()
    ax.imshow(data_array, cmap=custom_cmap, norm=norm, aspect=1, origin='lower', extent=[0, 8, 0, 12],
              interpolation='nearest')

    # 圆
    center = (4, 6)  # Center of the circle
    radius = 1  # Radius of the circle in cm
    circle = mpatches.Circle(center, radius, edgecolor='black', facecolor='none', linewidth=1)
    ax.add_patch(circle)

    # 色卡
    cbar = plt.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=custom_cmap), ax=ax, boundaries=bounds, ticks=bounds)
    ax.xaxis.set_ticks_position('top')  # 将X坐标轴移到上面
    ax.invert_yaxis()
    plt.show()
